import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

// Key is sorted word, e.g. "abdeils"
// Value is actual word, e.g. "disable"
// Map(Key, Value)

public class WordIndex {

	// Create HashMap called "index". This is a constructor...?
	private static Map<String, String> index = new HashMap<String, String>();

	// Main method
	public static void main(String[] args) {

		// Declare variables
		String userInput = null;
		String sortedInput = null;
		String indexAnswer = null;

		try {
			// Read 10kwords.txt file
			FileReader fileReader = new FileReader("10kwords.txt");

			// Create WordIndex object called "idx" created by fileReader (aka
			// 10kwords.txt file)
			WordIndex idx = new WordIndex(fileReader);

			// Ask user to enter scrambled word & look for "END" to quick
			// program. (Do while loop)
			do {
				// Get user input
				userInput = getInput();

				// End program if the user types "end"
				if (userInput.equalsIgnoreCase("end")) {
					break;
				}

				// Sort user input
				sortedInput = sort(userInput);

				// See if user input already exists
				indexAnswer = lookup(sortedInput);
				if (indexAnswer != null) {
					System.out.println("I believe I have found your word:  "
							+ indexAnswer);
					System.out
							.println("You may enter 'End' to quit this program.");
					System.out.println();
				} else {
					System.out.println("I am unable to find your word.");
					System.out
							.println("You may enter 'End' to quit this program.");
					System.out.println();
				}

			} while (!userInput.equalsIgnoreCase("END"));

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	// Get user input (method)
	public static String getInput() {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter a scrambled word:  ");
		String userInput = sc.nextLine();
		userInput = userInput.toLowerCase();
		userInput = userInput.trim();
		return userInput;

	}

	// Look up scrambled word and return matching word
	public static String lookup(String scrambled) {
		// Declare variables
		String indexWord;

		// If word is in HashMap, then return word.
		if (index.get(scrambled) != null) {
			indexWord = index.get(scrambled);
			return indexWord;

			// Else return null
		} else {
			indexWord = null;
			return indexWord;
		}
	}

	static String sort(String str) {
		byte[] bytes = str.getBytes();
		Arrays.sort(bytes);
		return new String(bytes);
	}

	public WordIndex(InputStreamReader isr) {
		try {
			BufferedReader bufferedReader = new BufferedReader(isr);
			String word = bufferedReader.readLine();
			while (word != null) {
				addToIndex(word);
				word = bufferedReader.readLine();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void addToIndex(String word) {

		// Declare variable
		String sortedWord = null;

		// Sort word
		sortedWord = sort(word);

		// If sorted word already exists, add to Value
		if (index.containsKey(sortedWord)) {
			index.put(sortedWord, index.get(sortedWord) + "," + word);

			// Else add word into HashMap
		} else {
			index.put(sort(word), word);
		}
	}

}
